package core;

import gitobject.Blob;
import gitobject.Commit;
import gitobject.GitObject;
import gitobject.Tree;
import repository.Repository;

import java.io.File;
import java.io.IOException;

public class JitCatFile {
    public static void catFile(String objectId) throws IOException {
        GitObject object = GitObject.deserialize(objectId);
        if(object != null){
            if(object.getFmt().equals("commit")){
                Commit commit = (Commit)object;
                System.out.println(commit.getValue());
            }
            if(object.getFmt().equals("tree")){
                Tree tree = (Tree)object;
                System.out.println(tree.getValue());
            }
            else if(object.getFmt().equals("blob")){
                Blob blob = (Blob)object;
                System.out.println(blob.getName());
                System.out.println("--------------------");
                int len = blob.getValue().length();
                if(len > 80){
                    String startValue = blob.getValue().substring(0, 40);
                    String endValue = blob.getValue().substring(len-40);
                    System.out.println(startValue);
                    System.out.println("...\r\n...\r\n...");
                    System.out.println(endValue);
                }
                else {
                    System.out.println(blob.getValue());
                }
            }
        }
    }
}
